﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;

namespace QuanLyKhachSan
{
    public partial class frmQuanLyLoaiPhong : Form
    {
        // Khai bao bien toan cuc 
        int thaoTac = 0;            //bien luu tru cho biet Thao Tac(1: them, 2: update,3:Xoa)
        List<string> dsTenLoaiPhong = new List<string>();
        List<float> dsDonGiaLoaiPhong = new List<float>();
        public frmQuanLyLoaiPhong()
        {
            InitializeComponent();
        }
        public void HienThiThongTin(int thaotac, string tenLoai,float donGia)
        {
            // Cac button
            thaoTac = 0;
            btnHuy.Visible = false;
            btnLuu.Enabled = false;
            txtTenLoaiPhong.Enabled = false;
            txtDonGia.Enabled = false;

            btnThoát.Visible = true;
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
           
            DataTable dt = new DataTable();
            dt = LoaiPhongBUS.LayDanhSachLoaiPhongTheoTenLoaiPhong(tenLoai);
            dgvDanhSachLoaiPhong.DataSource = dt;

            if (thaotac == 1)
            {
                dsTenLoaiPhong.Add(tenLoai);
                dsDonGiaLoaiPhong.Add(donGia);
                cboLoaiPhong.Items.Add(tenLoai);
                cboLoaiPhong.Text = tenLoai;
            }
            else if (thaotac == 2)
            {
                dsTenLoaiPhong.Clear();
                cboLoaiPhong.Items.Clear();
                dsDonGiaLoaiPhong.Clear();
                DataTable dt1 = new DataTable();
                dt1 = LoaiPhongBUS.LayDanhSachLoaiPhong();

                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        DataRow dr = dt1.Rows[i];
                        string tenLoaiP = dr["TenLoaiPhong"].ToString();

                        cboLoaiPhong.Items.Add(tenLoaiP);
                        dsTenLoaiPhong.Add(tenLoaiP);
                        float dGia = float.Parse(dr["DonGia"].ToString());
                        dsDonGiaLoaiPhong.Add(dGia);
                    }
                    cboLoaiPhong.Text = tenLoai;
                }

            }


        }
        private void frmQuanLyLoaiPhong_Load(object sender, EventArgs e)
        {
            
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;

            // Cac button xu ly
            thaoTac = 0;
            btnThoát.Visible = true;
            btnHuy.Visible = false;
            btnLuu.Enabled = false;
            txtTenLoaiPhong.Enabled = false;
            txtDonGia.Enabled  = false;

            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            
            // Hien thi thong tin DataGridView
            DataTable dt = LoaiPhongBUS.LayDanhSachLoaiPhong();
            dgvDanhSachLoaiPhong.DataSource = dt;
            
            // Hien thi thong tin len combobox
            dsTenLoaiPhong.Clear();
            dsDonGiaLoaiPhong.Clear();
            cboLoaiPhong.Items.Clear();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    string tenLoai = dr["TenLoaiPhong"].ToString();

                    cboLoaiPhong.Items.Add(tenLoai);
                    dsTenLoaiPhong.Add(tenLoai);
                    // Xu ly don gia
                   
                    float dGia = float.Parse(dr["DonGia"].ToString());
                    dsDonGiaLoaiPhong.Add(dGia);
                }
                cboLoaiPhong.SelectedIndex = 0;
            }

        }
        // Cac ham xu ly lien quan
        // Kiem tra xem tenLoaiPhong,don gia da ton tai trong csdl hay chua
        public bool KiemTraTheoLoaiPhong(string tenLoai,float donGia)
        {
            foreach (string ten in dsTenLoaiPhong)
            {
                if (ten == tenLoai)
                {
                    MessageBox.Show("Ten Loai Phong Da Co Trong He Thong","Thong Bao");
                    return false;
                }
            }
            foreach (float  dGia in dsDonGiaLoaiPhong)
            {
                if (dGia  == donGia)
                {
                    MessageBox.Show("Don Gia Da Co Trong He Thong","Thong Bao");
                    return false;
                }
            }
            return true ;
        }
        public bool KiemTraCapNhat(string tenBD, string tenSau, float dGiaBD, float dGiaSau)
        {
            int i = 0;
            foreach (string ten in dsTenLoaiPhong)
            {
                if (ten == tenBD )
                {
                    dsTenLoaiPhong.RemoveAt(i);
                    break;
                }
                i++;
            }
            i = 0;
            foreach (float dGia in dsDonGiaLoaiPhong)
            {
                if (dGia == dGiaBD)
                {
                    dsDonGiaLoaiPhong.RemoveAt(i);
                    break;
                }
                i++;
            }
            return KiemTraTheoLoaiPhong(tenSau, dGiaSau);
        }
        // Kiem tra nhap lieu tu ban phim
        public bool KiemTraDuLieuNhap()
        {
            if (string.IsNullOrEmpty(txtTenLoaiPhong.Text) == true)
            {
                MessageBox.Show("Ten Loai Phong Khong Duoc De Trong","Thong Bao");
                txtTenLoaiPhong.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtDonGia.Text) == true)
            {
                MessageBox.Show("Don Gia Loai Phong Khong Duoc De Trong","Thong Bao");
                txtDonGia.Focus();
                return false;
            }
            if (LoaiPhongDTO.KiemTraTenLoaiPhong(txtTenLoaiPhong.Text) == false)
            {
                MessageBox.Show("Ten Loai Phong khong hop le ", "Thong bao ");
                txtTenLoaiPhong.Focus();
                return false;
            }
            
            if (LoaiPhongDTO.KiemTraDonGia(txtDonGia.Text) == false)
            {
                MessageBox.Show("Don gia khong duoc nhap chu", "Thong bao ");
                txtDonGia.Focus();
                return false;
            }
            
            return true;
        }
        public LoaiPhongDTO LayThongTinTuTextBox()
        {
            LoaiPhongDTO loaiPhongdto = new LoaiPhongDTO();
            if (string.IsNullOrEmpty(txtMaLoaiPhong.Text) == false)
            {
                loaiPhongdto.MaLoaiPhong = int.Parse(txtMaLoaiPhong.Text);
            }
            loaiPhongdto.TenLoaiPhong = txtTenLoaiPhong.Text;
            loaiPhongdto.DonGia = int.Parse(txtDonGia.Text);
            return loaiPhongdto;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            thaoTac = 1;
            // Hien thi cac thong tin
            txtTenLoaiPhong.Enabled = true;
            txtDonGia.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;
            // An thong tin
            btnThoát.Visible = false;
            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            thaoTac = 2;
            // Hien thi cac thong tin
            txtTenLoaiPhong.Enabled = true;
            txtDonGia.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;
            // An cac thong tin
            btnThoát.Visible = false;
            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bool kq = false;
            LoaiPhongDTO loaiPhongdto = new LoaiPhongDTO();
            loaiPhongdto.MaLoaiPhong =int.Parse(txtMaLoaiPhong.Text);
            DialogResult result = MessageBox.Show("Ban chac chan muon xoa khong?", "Thong Bao", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                kq = LoaiPhongBUS.Delete(loaiPhongdto);    
            }
            if (kq == true)
            {
                MessageBox.Show("Xoa Loai Phong thanh cong ", "Thong bao ");
                frmQuanLyLoaiPhong_Load(sender, e); // load lai form
            }
            else
            {
                MessageBox.Show("Xoa Loai Phong khong thanh cong ", "Thong bao ");
                dgvDanhSachLoaiPhong.Focus();
            }
            // Hien cac thong tin
            btnThoát.Visible = true;
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
        }
        
        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool kq = false; ;
            if (KiemTraDuLieuNhap() == true)
            {
                // Lay thong tin tu form
                LoaiPhongDTO loaiPhongdto = LayThongTinTuTextBox();

                // Them 1 loai phong
                if (thaoTac == 1)
                {
                    if (KiemTraTheoLoaiPhong(loaiPhongdto.TenLoaiPhong,loaiPhongdto.DonGia ) == false)
                    {
                        kq = false;
                        txtTenLoaiPhong.Focus();

                    }
                    else
                    {
                        kq = LoaiPhongBUS.Insert(loaiPhongdto);
                    }
                }
                else       
                {
                    // Cap nhat thong tin loai phong
                    if (thaoTac == 2)
                    {
                        string tenBD = dgvDanhSachLoaiPhong.SelectedRows[0].Cells[1].Value.ToString();
                        float dGiaBD=float.Parse(dgvDanhSachLoaiPhong.SelectedRows[0].Cells[2].Value.ToString());
                        if (KiemTraCapNhat(tenBD, loaiPhongdto.TenLoaiPhong, dGiaBD, loaiPhongdto.DonGia) == true)
                        {
                            kq = LoaiPhongBUS.Update(loaiPhongdto);
                        }
                        else
                            kq = false;
                    }
                    
                }
                if (kq == true)
                {
                    MessageBox.Show("Luu thong tin Loai Phong thanh cong ", "Thong bao ");
                    HienThiThongTin(thaoTac, loaiPhongdto.TenLoaiPhong,loaiPhongdto.DonGia );
                }
                else
                {
                    MessageBox.Show("Luu thong tin Loai Phong khong thanh cong ", "Thong bao ");
                    txtTenLoaiPhong.Focus();
                }
                
            }
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            thaoTac = 0;
            // An cac thong tin
            btnHuy.Visible  = false;
            btnLuu.Enabled = false;
            // Hien cac thong tin
            btnThoát.Visible = true;
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;

        }
       

       
        private void btnThoát_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboLoaiPhong_TextChanged(object sender, EventArgs e)
        {
            int index = cboLoaiPhong.SelectedIndex;
            if (index != -1)
            {
                string tenTK = cboLoaiPhong.Text;
                DataTable dt = LoaiPhongBUS.LayDanhSachLoaiPhongTheoTenLoaiPhong(tenTK);
                dgvDanhSachLoaiPhong.DataSource = dt;
            }
        }

        private void dgvDanhSachLoaiPhong_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachLoaiPhong.SelectedRows.Count > 0)
            {
                txtMaLoaiPhong.Text = dgvDanhSachLoaiPhong.SelectedRows[0].Cells[0].Value.ToString();
                txtTenLoaiPhong.Text = dgvDanhSachLoaiPhong.SelectedRows[0].Cells[1].Value.ToString();
                txtDonGia.Text = dgvDanhSachLoaiPhong.SelectedRows[0].Cells[2].Value.ToString();
                cboLoaiPhong.Text = dgvDanhSachLoaiPhong.SelectedRows[0].Cells[1].Value.ToString();
            }
            else
            {
                txtMaLoaiPhong.Text = "";
                txtTenLoaiPhong.Text = "";
                txtDonGia.Text = "";
                
            }
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        

        
    }
}